// var mpu = require('mpu6050-dmp');

// if (mpu.initialize()) {
//   setInterval(function () {
//     console.log('getRotation = ',mpu.getRotation());
//     console.log('getAttitude = ',mpu.getAttitude());
//   }, 50);
// }


let ws = require("nodejs-websocket");

let myInver = null;
let falg = false;
var server = ws.createServer(function(conn){
  // console.log('server = ',server);
  // console.log('conn = ',conn);
  conn.on("text", function (str) {
    console.log("收到的信息为:"+str);
    flag = true;
    sendStr(conn);
  })

  conn.on("close", function (code, reason) {
    console.log("关闭连接");
  });
  conn.on("error", function (code, reason) {
    console.log("异常关闭");
    flag = false;
    if (!flag && myInver != null) {
      clearInterval(myInver);
      myInver =  null;
    } 
  });
}).listen(3000)
console.log("WebSocket建立完毕")


function sendStr(conn) {
  myInver = setInterval(function(){
    conn.send(count + '');
  },500);
}